/*******************************************************************
Copyright (C) 2016 AMPL Optimization, Inc.; written by David M. Gay.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that the copyright notice and this permission notice and warranty
disclaimer appear in supporting documentation.

The author and AMPL Optimization, Inc. disclaim all warranties with
regard to this software, including all implied warranties of
merchantability and fitness.  In no event shall the author be liable
for any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether in an
action of contract, negligence or other tortious action, arising out
of or in connection with the use or performance of this software.
*******************************************************************/

/* Many Unix systems have a clock_gettime() that returns time with a */
/* resolution of nanoseconds or a getrusage() that returns time with */
/* a resolution of microseconds.  Compile with -DNO_RUSAGE to avoid */
/* avoid using both, and with -DNO_CLOCK_GETTIME to avoid using */
/* clock_gettime(). */

#ifdef KR_headers
#define Void /*void*/
#else
#define Void void
#endif
#ifdef __cplusplus
extern "C" double xectim_(Void);
#endif

#ifdef __STDC__
#define ASSUME_STDC
#endif
#ifdef _WIN32
#undef  NO_RUSAGE
#define NO_RUSAGE
#undef  ASSUME_STDC
#define ASSUME_STDC
#include <windows.h>
#endif
#ifdef __APPLE__
#define NO_CLOCK_GETTIME
#endif

#ifndef NO_RUSAGE /*{{*/

#ifndef NO_CLOCK_GETTIME /*{{*/

#include <time.h>

 double
xectim_(Void)
{
	struct timespec T;
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &T);
	return T.tv_sec + 1e-9*T.tv_nsec;
	}

#else /*}NO_CLOCK_GETTIME{*/

#include <sys/time.h>
#include <sys/resource.h>

 double
xectim_(Void)
{
	struct rusage R;
	getrusage(RUSAGE_SELF, &R);
	return R.ru_utime.tv_sec + R.ru_stime.tv_sec
		+ 1e-6*(R.ru_utime.tv_usec + R.ru_stime.tv_usec);
	}

#endif /*}}*/

#else /* }NO_RUSAGE{ */

#if defined(ASSUME_STDC) && !defined(Clocks_to_seconds) /*{{*/
#include "time.h"

 double
xectim_(Void)
{
#ifdef _WIN32
	FILETIME PrCT, PrET, PrKT, PrUT;
	double st, ut;
	static HANDLE mypid;
	static int first = 1;

	if (first) {
		first = 0;
		mypid = GetCurrentProcess();
		}
	if (GetProcessTimes(mypid, &PrCT, &PrET, &PrKT, &PrUT)) {
		ut = (PrUT.dwLowDateTime
				+ 4294967296.*PrUT.dwHighDateTime)*1e-7;
		st = (PrKT.dwLowDateTime
				+ 4294967296.*PrKT.dwHighDateTime)*1e-7;
		return st + ut;
		}
#endif
	return (double)clock() / CLOCKS_PER_SEC;
	}

#else /*}defined(__STDC__) && !defined(Clocks_to_seconds){*/
#include "sys/types.h"
#include "sys/times.h"
#ifndef Clocks_to_seconds /*{*/
#ifdef CRAY /*{{*/
#define Clocks_to_seconds * 9.5e-9
#else /*}CRAY{*/

#ifndef HZ /*{*/
#include "limits.h"
#ifdef CLOCKS_PER_SEC /*{{*/
#define HZ (double)(CLOCKS_PER_SEC)
#else /*}CLOCKS_PER_SEC{*/
#ifdef CLK_TCK /*{{*/
#define HZ (double)(CLK_TCK)
#else /*}CLK_TCK{*/
#ifdef mips /*{{*/
#define HZ 100.0
#else /*}mips{*/
#define HZ 60.0
#endif /*}}mips*/
#endif /*}}CLK_TCK*/
#endif /*}}CLOCKS_PER_SEC*/
#endif /*}HZ*/
#define Clocks_to_seconds / HZ
#endif /*}}CRAY*/
#endif /*}Clocks_to_seconds*/

double xectim_(Void)
{
	struct tms t;

	times(&t);
	return((t.tms_utime + t.tms_stime) Clocks_to_seconds);
	}
#endif /*}}defined(__STDC__) && !defined(Clocks_to_seconds)*/
#endif /*}}NO_RUSAGE*/
